package com.huanzhidadi.single.completablefuture;

import java.util.concurrent.*;

/**
 * 计算完成的后续操作 返回自定义结果
 *  <U> CompletableFuture<U> thenApplyAsync(Function<? super T,? extends U> fn, Executor executor)
 */
public class ThenApplyTest {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("子线程" + Thread.currentThread().getName());
            return "123";
        }, executorService).thenApplyAsync(value -> {
            System.out.println("thenApply子线程" + Thread.currentThread().getName());
            executorService.shutdown();
            return value + "thenApply";
        },executorService);
        String s = null;
        try {
            s = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(s+" 主线程结束" + Thread.currentThread().getName());
    }
}
